//
//  FailViewController.m
//  SecretPhoto
//
//  Created by administrator on 12-9-14.
//  Copyright (c) 2012年 com.ptc. All rights reserved.
//

#import "FailViewController.h"
#import "ViewController.h"

@interface FailViewController ()


@end

@implementation FailViewController
@synthesize imageView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)takePicture:(id)sender;
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        NSArray *temp_MediaTypes = [UIImagePickerController availableMediaTypesForSourceType:picker.sourceType];
        picker.mediaTypes = temp_MediaTypes;
        picker.delegate = self;
        picker.allowsImageEditing = YES;
    }
    
    [self presentModalViewController:picker animated:YES];
    [picker release];
    
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    
    
    NSDateFormatter *time = [[[NSDateFormatter alloc] init ]autorelease];  ///以时间命名文件
    [time setDateFormat:@"yyyyMMddHHmmssSS"];
    NSString *timenow = [time stringFromDate:[NSDate date]];
    NSString *imagename=[NSString stringWithFormat:@"%@.jpg",timenow];
    
    
    
    
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
    if ([mediaType isEqualToString:@"public.image"]){
        
        UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
        NSLog(@"found an image");
        
        //   NSString *imageFile = [documentsDirectory stringByAppendingPathComponent:@"Image/temp.jpg"];
        NSString *imageFile = [documentsDirectory stringByAppendingPathComponent:imagename];
        
        NSLog(@"%@", imageFile);
        
        success = [fileManager fileExistsAtPath:imageFile];
        if(success) {
            success = [fileManager removeItemAtPath:imageFile error:&error];
        }
        
        imageView.image = image;
        
        //        [UIImageJPEGRepresentation(image, 1.0f) writeToFile:imageFile atomically:YES];
        
        
        
        
        UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(imageSavedToPhotosAlbum: didFinishSavingWithError: contextInfo:), nil);
        
        success = [fileManager fileExistsAtPath:imageFile];
        if(success) {
            success = [fileManager removeItemAtPath:imageFile error:&error];
        }
        else{
            NSLog(@"ok");
        }
        
    }
    else{
        NSLog(@"非照片");
    }
    
    [picker dismissModalViewControllerAnimated:YES];
}


- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    NSString *message;
    NSString *title;
    if (!error) {
        title = NSLocalizedString(@"消息", @"");
        message = NSLocalizedString(@"您的图片已存到照片库", @"");
    } else {
        title = NSLocalizedString(@"储存失败", @"");
        message = [error description];
    }
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                    message:message
                                                   delegate:nil
                                          cancelButtonTitle:NSLocalizedString(@"OK", @"")
                                          otherButtonTitles:nil];
    [alert show];
    [alert release];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    
    [picker dismissModalViewControllerAnimated:YES];
    
}



- (IBAction)back:(id)sender {
    ViewController *viewController =[[[ViewController alloc]initWithNibName:@"viewController" bundle:nil]autorelease];
    [self.view.window setRootViewController:viewController];
}

- (void)dealloc {
    [imageView release];
    [super dealloc];
}


@end
